package com.rrd.hive.udf;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

public class MaskOffCodeUDF extends UDF {

    public Text evaluate(String str, String type) {
        Text text = new Text();
        if (str == null || str.length() == 0) {
            return text;
        }
        if (type.equals("pid")) {
            if (str.length() > 14) {
                str = str.substring(0, 10) + "****" + str.substring(14);
            }
        } else if (type.equals("mobile")) {
            if (str.length() > 8) {
                str = str.substring(0, 4) + "****" + str.substring(8);
            }
        } else if (type.equals("telphone")) {
            if (str.length() > 4) {
                str = str.substring(0, 5) + "****";
            }
        } else if (type.equals("telphone")) {
            if (str.length() > 4) {
                str = str.substring(0, 5) + "****";
            }
        } else if (type.equals("email")) {
            if (str.indexOf("@") > 0) {
                str = "****" + str.substring(str.indexOf("@"));
            }
        } else if (type.equals("bank")) {
            if (str.length() > 4) {
                str = "********" + str.substring(str.length() - 4);
            }
        } else if (type.equals("home")) {
            Pattern p = Pattern.compile(".*[0-9]+(楼|号楼|院)(.*)");
            Matcher m = p.matcher(str);
            if (m.find()) {
                System.out.println(m.group(2));
                str = str.replace(m.group(2), "****");
            }
        }
        text.set(str);
        return text;

    }

    public static void main(String[] args) {
        String str = "某某小区，sdf2单元201";
        System.out.println(str);
        Pattern p = Pattern.compile(".*[0-9]+(楼|号楼|院)(.*)");
        Matcher m = p.matcher(str);

        if (m.find()) {
            System.out.println(m.group(2));
            str = str.replace(m.group(2), "****");
        }

        System.out.println(str);
    }
}
